//
//  YZLCarTypeMessageViewController.m
//  Car
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLCarTypeMessageViewController.h"
#import "HTTPTOOL.h"
#import <UIImageView+WebCache.h>

@interface YZLCarTypeMessageViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) NSMutableArray *typenameArr;
@property (nonatomic, strong) NSMutableArray *configArr;
@property (nonatomic, strong) UIView *activity;
@property (nonatomic, strong) UIActivityIndicatorView *activityView;

@end

@implementation YZLCarTypeMessageViewController

#pragma mark - 数据处理
- (void)dataHandle {
    
    NSString *str = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getCarParametersById.php?carId=%@", self.carId];
    [HTTPTOOL GETWithURL:str body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 80, 60)];
        [image sd_setImageWithURL:[NSURL URLWithString:[result objectForKey:@"carImage"]] placeholderImage:[UIImage imageNamed:@"D0"]];
        [self.headerView addSubview:image];
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 20, self.view.frame.size.width - 120, 30)];
        nameLabel.text = [result objectForKey:@"carName"];
        nameLabel.textAlignment = 1;
        [self.headerView addSubview:nameLabel];
        
        NSArray *arr = [result objectForKey:@"config"];
        self.typenameArr = [NSMutableArray array];
        self.configArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            NSString *str1 = @"    (●标配  ○选配  -无  ▲待查)";
            NSString *str2 = [[dic objectForKey:@"typename"] stringByAppendingString:str1];
            [self.typenameArr addObject:str2];
            [self.configArr addObject:dic];
            
        }
        
        [self.tableView reloadData];
        self.activity.hidden = YES;
        [self.activityView stopAnimating];
        
    } fail:^(NSError *error) {
        
    }];
    
}

#pragma mark - 创建tableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
    self.tableView.tableHeaderView = self.headerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"参数配置";
   
    [self dataHandle];
    [self createTableView];
    
    //返回
    [self skipReturnWithTarget:self];
    
    //bar颜色
    [self navigationbarColor];
    
    /* 指示器 */
    self.activity = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64)];
    self.activity.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.activity];
    self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityView.color = [UIColor lightGrayColor];
    self.activityView.center = CGPointMake(self.activity.frame.size.width / 2.f, self.activity.frame.size.height / 2.f - 64);
    [self.activity addSubview:self.activityView];
    [self.activityView startAnimating];
}

#pragma mark - tableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSDictionary *dic = [self.configArr objectAtIndex:section];
    NSArray *arr = [dic objectForKey:@"result"];
    return arr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.typenameArr.count;
}

//调节titleForHeaderInSection字体颜色和字体大小
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 20)];
    customView.backgroundColor = [UIColor colorWithWhite:0.950 alpha:1.000];
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 5.0, self.view.frame.size.width - 10, 20)];
    headerLabel.backgroundColor = [UIColor colorWithWhite:0.950 alpha:1.000];
    headerLabel.opaque = YES;//透明度
    headerLabel.textColor = [UIColor colorWithWhite:0.500 alpha:1.000];
    headerLabel.font = [UIFont boldSystemFontOfSize:15];
    headerLabel.text = [self.typenameArr objectAtIndex:section];
    [customView addSubview:headerLabel];
    
    return customView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"reuse"];
    }
    NSDictionary *dic = [self.configArr objectAtIndex:indexPath.section];
    NSArray *arr = [dic objectForKey:@"result"];
    cell.textLabel.text = [[arr objectAtIndex:indexPath.row] objectForKey:@"langname"];
    cell.detailTextLabel.text = [[arr objectAtIndex:indexPath.row] objectForKey:@"value"];
    cell.detailTextLabel.textColor = [UIColor blueColor];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
    return cell;
}

#pragma mark - 隐藏tabBar
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
